//
//  PGRootTool.m
//
//  Created by wdj on 15/11/25.
//  Copyright (c) 2015年 pg. All rights reserved.
//

#import "PGRootTool.h"
#import "PGTabBarController.h"
#import "PGGuideViewController.h"
#import "LoginVC.h"
#import "Loger3DLoginViewController.h"

@implementation PGRootTool

+ (UIViewController *)chooseRootController
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

    NSString *currentVer = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    NSString *lastVer = [userDefault objectForKey:@"version"];
    
    UIViewController *rootVc;
    
    if ([currentVer isEqualToString:lastVer]) {
        //如果有登录就自动登录,进入到主页
        //版本正确直接进入主页
        NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
        
//        PGTabBarController *pGTabBarController = [[PGTabBarController alloc] init];
//        rootVc = pGTabBarController;
        
//        没有token进入登录页,目测暂时不需要
        if (token != nil) {
            PGTabBarController *pGTabBarController = [[PGTabBarController alloc] init];
            rootVc = pGTabBarController;
        } else {
            //storyboard登录界面
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
            LoginVC *loginVC = [storyboard instantiateInitialViewController];
            rootVc = loginVC;
            //纯代码登陆界面
//            Loger3DLoginViewController *loginVC = [[Loger3DLoginViewController alloc] init];
//            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
//            rootVc = nav;
            
        }
    }else {
        //如果是第一次或者更新了应用就去引导页
        rootVc = [[PGGuideViewController alloc] init];
        [[NSUserDefaults standardUserDefaults] setObject:currentVer forKey:@"version"];
        //同步写入
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    return rootVc;
}

@end
